package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_activity_type;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_activity_type;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_activity_typeSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_activity_typeService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[活动类型] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_activity_typeServiceImpl implements IMail_activity_typeService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_activity_typeClientService mail_activity_typeClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_activity_typeClientService.removeBatch(mail_activity_types) ;

    }

    @Override
    public void createBatch(List<Mail_activity_type> mail_activity_types, int batchSize) {

                        //mail_activity_typeClientService.createBatch(mail_activity_types,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_activity_type clientModel = mail_activity_typeClientService.createModel();
        clientModel.setId(id);
        mail_activity_typeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mail_activity_type get(Integer id) {
        Imail_activity_type clientModel = mail_activity_typeClientService.createModel();
        clientModel.setId(id);
        mail_activity_typeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_activity_type get(Mail_activity_type mail_activity_type) {
        Mail_activity_type retmail_activity_type = this.get(mail_activity_type.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_activity_type.getClass(), mail_activity_type.getClass(), false);
        copier.copy(retmail_activity_type, mail_activity_type, null);
        return mail_activity_type ;
    }

    @Override
    public boolean create(Mail_activity_type mail_activity_type) {
        boolean bOk = false ;
        Imail_activity_type clientModel = this.convert2Model(mail_activity_type,null);
        mail_activity_typeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_activity_type);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Mail_activity_type> mail_activity_types, int batchSize) {

        //mail_activity_typeClientService.updateBatch(mail_activity_types,batchSize) ;

    }

    @Override
    public boolean update(Mail_activity_type mail_activity_type) {
        boolean bOk = false ;
        Imail_activity_type clientModel = this.convert2Model(mail_activity_type,null);
        mail_activity_typeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_activity_type) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_activity_type> searchDefault(Mail_activity_typeSearchContext context) {
        List<Mail_activity_type> list = new ArrayList<Mail_activity_type>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity_type> clientModelList = mail_activity_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity_type>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据活动类型查询
     */
    @Override
    public Page<Mail_activity_type> selectByOdoo_default_next_type(Integer id){
        Mail_activity_typeSearchContext context = new Mail_activity_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity_type> list = new ArrayList<Mail_activity_type>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity_type> clientModelList = mail_activity_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity_type> selectByOdoo_create(Integer id){
        Mail_activity_typeSearchContext context = new Mail_activity_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity_type> list = new ArrayList<Mail_activity_type>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity_type> clientModelList = mail_activity_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_activity_type> selectByOdoo_write(Integer id){
        Mail_activity_typeSearchContext context = new Mail_activity_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_activity_type> list = new ArrayList<Mail_activity_type>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_activity_type> clientModelList = mail_activity_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_activity_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_activity_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_activity_type convert2Model(Mail_activity_type domain , Imail_activity_type model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_activity_typeClientService.createModel() ;
        }

         if(domain.getRes_model_changeDirtyFlag())
            model.setRes_model_change(domain.getRes_model_change());
         if(domain.getSummaryDirtyFlag())
            model.setSummary(domain.getSummary());
         if(domain.getForce_nextDirtyFlag())
            model.setForce_next(domain.getForce_next());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCategoryDirtyFlag())
            model.setCategory(domain.getCategory());
         if(domain.getIconDirtyFlag())
            model.setIcon(domain.getIcon());
         if(domain.getDelay_countDirtyFlag())
            model.setDelay_count(domain.getDelay_count());
         if(domain.getPrevious_type_idsDirtyFlag())
            model.setPrevious_type_ids(domain.getPrevious_type_ids());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getDelay_fromDirtyFlag())
            model.setDelay_from(domain.getDelay_from());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getMail_template_idsDirtyFlag())
            model.setMail_template_ids(domain.getMail_template_ids());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getRes_model_idDirtyFlag())
            model.setRes_model_id(domain.getRes_model_id());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDecoration_typeDirtyFlag())
            model.setDecoration_type(domain.getDecoration_type());
         if(domain.getNext_type_idsDirtyFlag())
            model.setNext_type_ids(domain.getNext_type_ids());
         if(domain.getDelay_unitDirtyFlag())
            model.setDelay_unit(domain.getDelay_unit());
         if(domain.getInitial_res_model_idDirtyFlag())
            model.setInitial_res_model_id(domain.getInitial_res_model_id());
         if(domain.getDefault_next_type_id_textDirtyFlag())
            model.setDefault_next_type_id_text(domain.getDefault_next_type_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getDefault_next_type_idDirtyFlag())
            model.setDefault_next_type_id(domain.getDefault_next_type_id());
        return model ;
    }

    public Mail_activity_type convert2Domain( Imail_activity_type model ,Mail_activity_type domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_activity_type();
        }

        if(model.getRes_model_changeDirtyFlag())
            domain.setRes_model_change(model.getRes_model_change());
        if(model.getSummaryDirtyFlag())
            domain.setSummary(model.getSummary());
        if(model.getForce_nextDirtyFlag())
            domain.setForce_next(model.getForce_next());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCategoryDirtyFlag())
            domain.setCategory(model.getCategory());
        if(model.getIconDirtyFlag())
            domain.setIcon(model.getIcon());
        if(model.getDelay_countDirtyFlag())
            domain.setDelay_count(model.getDelay_count());
        if(model.getPrevious_type_idsDirtyFlag())
            domain.setPrevious_type_ids(model.getPrevious_type_ids());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getDelay_fromDirtyFlag())
            domain.setDelay_from(model.getDelay_from());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getMail_template_idsDirtyFlag())
            domain.setMail_template_ids(model.getMail_template_ids());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getRes_model_idDirtyFlag())
            domain.setRes_model_id(model.getRes_model_id());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDecoration_typeDirtyFlag())
            domain.setDecoration_type(model.getDecoration_type());
        if(model.getNext_type_idsDirtyFlag())
            domain.setNext_type_ids(model.getNext_type_ids());
        if(model.getDelay_unitDirtyFlag())
            domain.setDelay_unit(model.getDelay_unit());
        if(model.getInitial_res_model_idDirtyFlag())
            domain.setInitial_res_model_id(model.getInitial_res_model_id());
        if(model.getDefault_next_type_id_textDirtyFlag())
            domain.setDefault_next_type_id_text(model.getDefault_next_type_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getDefault_next_type_idDirtyFlag())
            domain.setDefault_next_type_id(model.getDefault_next_type_id());
        return domain ;
    }


}
